package myUtils;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.springframework.web.multipart.MultipartFile;

import com.github.pagehelper.PageInfo;

public class Utils {
	public static String getUUID() {
        UUID uuid=UUID.randomUUID();
        String str = uuid.toString(); 
        String uuidStr=str.replace("-", "");
        return uuidStr;
    }
	
	public static String uploadFileHandler(
			// 文件的绝对路径ַ
			String rootPath, 
			MultipartFile file
	) {
		System.out.println(file);
		if (file!=null && !file.isEmpty()) {
            try {
                File dir = new File(rootPath);
                if (!dir.exists())
                    dir.mkdirs();
                int index = file.getOriginalFilename().indexOf(".");
                String type = file.getOriginalFilename().substring(index); //获取后缀名
                String name = file.getOriginalFilename().substring(0, index);// 获取文件名称
                String fileName = name + Utils.getTimeStamp() + type;
                File serverFile = new File(dir.getAbsolutePath() + File.separator + fileName);
                file.transferTo(serverFile);
                return fileName;
            } catch (Exception e) {
                return null;
            }
        } else {
            return null;
        }
	}
	
	public static String getTimeStamp() {
		return String.valueOf(System.currentTimeMillis());
	}
	
	public static String formatDate(Date time){ 
	    SimpleDateFormat formatter; 
	    formatter = new SimpleDateFormat ("yyyy-MM-dd"); 
	    String ctime = formatter.format(time); 
	    return ctime; 
	} 
}
